package com.zhou.piledriver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhou.piledriver.mapper.PdDeviceMapper;
import com.zhou.piledriver.mapper.PdMachMapper;
import com.zhou.piledriver.model.entity.PdDevice;
import com.zhou.piledriver.model.entity.PdMach;
import com.zhou.piledriver.model.entity.PdPile;
import com.zhou.piledriver.model.vo.PdDeviceVO;
import com.zhou.piledriver.model.vo.PdProjectVO;
import com.zhou.piledriver.service.PdDeviceService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ZhouShenBin
 * @date 2024/3/21 9:44
 * @description
 */

@Service
public class PdDeviceServiceImpl implements PdDeviceService {
@Resource
    PdDeviceMapper pdDeviceMapper;
@Resource
    PdMachMapper pdMachMapper;
    @Override
    public List<PdDeviceVO> list(String tenantId, String projectId) {
        List<PdDeviceVO> result = new ArrayList<>();
        LambdaQueryWrapper<PdDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(tenantId),PdDevice::getTenantId, tenantId)
                .eq(StringUtils.isNotBlank(projectId),PdDevice::getProjectId, projectId);
        List<PdDevice> devices = pdDeviceMapper.selectList(queryWrapper);
        result = devices.stream().map(device->{
            PdDeviceVO vo = new PdDeviceVO();
            BeanUtils.copyProperties(device,vo);
            PdMach mach = pdMachMapper.selectOne(new LambdaQueryWrapper<PdMach>().eq(PdMach::getDeviceId, device.getId()));
            if(!Objects.isNull(mach)){
                vo.setMachCode(mach.getCode());
                vo.setMachType(mach.getType());
            }
            return vo;
        }).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        return result;
    }
}
